'use strict';
/**
 * Created by FreezeSoul on 2015/9/30.
 */
define([
        'angular',
        'app/modules/source/define',
        'app/modules/source/services/source'
    ],
    function(angular) {
        angular.module('ad.source')
            .controller('adSourceEditController',['$scope','$uibModalInstance','adSourceService','item',function($scope,$uibModalInstance,service,item) {
                $scope.item = item;

                $scope.dataSourceTypes = adConfig.dataSourceTypes;

                $scope.ok = function () {
                    if(!item.datasource_id){
                        service.addItem($scope.item).then(function(data){
                            $uibModalInstance.close(data);
                        });
                    }else{
                        service.updateItem($scope.item).then(function(data){
                            $uibModalInstance.close(data);
                        });
                    }

                };

                $scope.cancel = function () {
                    $uibModalInstance.dismiss('cancel');
                };

                $scope.isTestSuccess = false;

                $scope.test = function(){
                    service.testSource($scope.item).then(function(data){
                        var obj = angular.fromJson(data);
                        if(obj.result == 'success'){
                            $scope.testInfo = '连接测试成功！';
                            $scope.isTestSuccess = true;
                        }else{
                            $scope.testInfo = '连接测试失败！' + (obj.info ? obj.info.error : "");
                            $scope.isTestSuccess = false;
                        }
                    });
                }
            }]);
    });